package com.sh.mytool.cv.controller;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.sh.common.extjs.CUDResult;
import com.sh.common.extjs.GetResult;
import com.sh.common.file.FileUtils;
import com.sh.common.page.QueryResult;
import com.sh.common.properties.PropertiesUtils;
import com.sh.common.web.ResponseHandle;
import com.sh.mytool.cv.domain.Video;
import com.sh.mytool.cv.domain.VideoFile;
import com.sh.mytool.cv.domain.VideoImage;
import com.sh.mytool.cv.service.VideoService;
import com.sh.common.file.FileBean;

@Controller
public class VideoController {

    @Resource(name="videoService")
    private VideoService service;

    public VideoService getService() {
        return service;
    }

    public void setService(VideoService service) {
        this.service = service;
    }

    @InitBinder
    public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
    }

    @RequestMapping(value = "/video/page", method = RequestMethod.POST)
    public void videoPage(@RequestParam(value="start") Integer start, @RequestParam(value="limit") Integer limit, HttpServletResponse response, PrintWriter out) throws Exception{
        QueryResult<Video> result = service.page("", null, start, limit);
        String json = ResponseHandle.handleJson("", "", result, true);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/video/edit", method = RequestMethod.POST)
    public void videoEdit(Video video, HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws Exception{
        Boolean isSuccess = true;
        VideoFile videoFile = null;
        VideoImage imageFile = null;
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String,MultipartFile> files = multipartRequest.getFileMap();
        for(Entry<String, MultipartFile> bean : files.entrySet()){
            String filedName =  bean.getKey();
            MultipartFile file = bean.getValue();
            if(file.getSize() > 0){
                if(filedName.equals("image")){
                    String imageRoot = PropertiesUtils.getValueByResourceBundle("project", "image.root");
                    String imagePath = request.getSession().getServletContext().getRealPath(imageRoot);
                    FileBean fileBean = FileUtils.uploadFile(file.getInputStream(), imagePath, file.getOriginalFilename());
                    imageFile = new VideoImage();
                    imageFile.setName(fileBean.getName());
                    imageFile.setPath(fileBean.getPath());
               }else if(filedName.equals("video")){
                   String videoRoot = PropertiesUtils.getValueByResourceBundle("project", "video.root");
                   String videoPath = request.getSession().getServletContext().getRealPath(videoRoot);
                   FileBean fileBean = FileUtils.uploadFile(file.getInputStream(), videoPath, file.getOriginalFilename());
                   videoFile = new VideoFile();
                   videoFile.setName(fileBean.getName());
                   videoFile.setPath(fileBean.getPath());
               }
            }
        }
        try {
            Long id = video.getId();
            if (id != null) {
                service.update(video, videoFile, imageFile);
            } else {
                service.insert(video, videoFile, imageFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        }
        String json = ResponseHandle.handleJson("", "", new CUDResult(isSuccess), false);
        response.setContentType("text/html;charset=UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/video/get", method = RequestMethod.POST)
    public void videonGet(@RequestParam(value="id") Long id, HttpServletResponse response, PrintWriter out) throws Exception{
        Video result = service.get(id);
        Boolean isSuccess = false;
        if(result != null){
            isSuccess = true;
        }
        String json = ResponseHandle.handleJson("", "", new GetResult(isSuccess, result), false);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/video/remove", method = RequestMethod.POST)
    public void videoRemove(@RequestParam(value="id") Long id, HttpServletResponse response, PrintWriter out) throws Exception{
        boolean isSuccess = true;
        try {
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        }
        String json = ResponseHandle.handleJson("", "", new CUDResult(isSuccess), false);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }
}
